package com.nb.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

public class LocalFileStorage {

    private static final String uploadDir = "uploads";

    public static String storeFile(MultipartFile file) throws IOException {
            // 生成唯一文件名防止覆盖
            String originalName = file.getOriginalFilename();
            String extension = originalName.substring(originalName.lastIndexOf("."));
            String uniqueFileName = UUID.randomUUID() + extension;

            Path targetPath = Paths.get(uploadDir).resolve(uniqueFileName);
            Files.createDirectories(targetPath.getParent());
            Files.copy(file.getInputStream(), targetPath);

            return uniqueFileName;

    }
}
